package g6.UddiRegistration;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import org.apache.juddi.v3.client.ClassUtil;
import org.apache.juddi.v3.client.config.UDDIClientContainer;
import org.apache.juddi.v3.client.transport.Transport;
import org.apache.juddi.v3_service.JUDDIApiPortType;
import org.uddi.api_v3.AuthToken;
import org.uddi.api_v3.BusinessInfo;
import org.uddi.api_v3.BusinessList;
import org.uddi.api_v3.FindBusiness;
import org.uddi.api_v3.GetAuthToken;
import org.uddi.api_v3.GetBusinessDetail;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIInquiryPortType;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.uddi.v3_service.UDDISecurityPortType;

/**
 * Hello world!
 *
 */
public class App 
{	
    private static boolean isRunning = false;    
    /*
    private static String myUrl1 = "http://localhost:8080/axis2/services/PaymentWSImp?wsdl";
    private static String myUrl2 = "http://localhost:8080/axis2/services/LoadInfo?wsdl";
    private static String myUrl3 = "http://localhost:8080/axis2/services/Warehouse?wsdl";
    */
    private static String uri = "http://vmjacobsen4.informatik.tu-muenchen.de:8080/axis2/services/listServices";
    
    private static String myUrl1 = "http://vmjacobsen4.informatik.tu-muenchen.de:8080/axis2/services/g6/PaymentWSImp?wsdl";
    private static String myUrl2 = "http://vmjacobsen4.informatik.tu-muenchen.de:8080/axis2/services/g6/HotelReservation?wsdl";
    private static String myUrl3 = "http://vmjacobsen4.informatik.tu-muenchen.de:8080/axis2/services/g6/WareHouse?wsdl";
    
    private static String webServiceName1 = "Payment";
    private static String webServiceName2 = "Hotel Reservation";
    private static String webServiceName3 = "Warehouse";
    
    private static String description1= "Online Payment in any bank";
    private static String description2 = "Manage your Hotel Reservations in any hotel";
    private static String description3 = "Control the warehouse of your company";
    
    private static String modelName1 = "OnlinePayment";
    private static String modelName2 = "HotelReservation";
    private static String modelName3 = "Warehouse";
    
    private static String modelDetails1 = "Web service implemented in JAVA";
    private static String modelDetails2= "JAXB and JAVA";
    private static String modelDetails3= "JAVA and Axis2";
        
    private static String[] urls;
    private static String[] webServiceNames;
    private static String[] descriptions;
    private static String[] modelNames;
    private static String[] modelDetails;
    
    private static String defaultBusinessEntityDescription = "Web Services @ TUM by Gramoz, Panos and Daniel";
    private static String defaultBusinessEntityName = "g6";
    
    public static void main( String[] args )
    {
    	urls = new String[3];
    	urls[0] = myUrl1;
    	urls[1] = myUrl2;
    	urls[2] = myUrl3;
    	
    	webServiceNames = new String[3];
    	webServiceNames[0] = webServiceName1;
    	webServiceNames[1] = webServiceName2;
    	webServiceNames[2] = webServiceName3;
    	
    	descriptions = new String[3];
    	descriptions[0] = description1;
    	descriptions[1] = description2;
    	descriptions[2] = description3;
    	
    	modelNames = new String[3];
    	modelNames[0] = modelName1;
    	modelNames[1] = modelName2;
    	modelNames[2] = modelName3;
    	
    	modelDetails = new String[3];
    	modelDetails[0] = modelDetails1;
    	modelDetails[1] = modelDetails2;
    	modelDetails[2] = modelDetails3;
    	
    	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));	
        System.out.println( "Starting..." );        
        JuddiManager juddiManager = new JuddiManager();
        juddiManager.initialize();
        try {
        	juddiManager.authenticateToServer();
			
			isRunning = true;
		} catch (DispositionReportFaultMessage e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        while(isRunning)
		{	
			try{
				String inputOption = input.readLine();
				handleInputUser(juddiManager, inputOption);				
			}catch(Exception ex){
				ex.printStackTrace();
				
			}			
		}
    }
    
    private static void handleInputUser(JuddiManager juddiManager, String inputOption) throws DispositionReportFaultMessage, RemoteException {
    	
    	String businessName = defaultBusinessEntityName;
    	
    	int index = 0;
    	try {
    		businessName = inputOption.split(" ")[1];
    		index = Integer.parseInt( inputOption.split(" ")[2]);
    	}
    	catch (Exception ex) {
    		
    	}
    	
    	if (inputOption.startsWith("all")) {
    		String key = juddiManager.createBussinessEntity(defaultBusinessEntityName, defaultBusinessEntityDescription);
    		for (int i = 0; i < webServiceNames.length; i++)
    			juddiManager.publishWebService(key, webServiceNames[i], urls[i], descriptions[i], uri, modelNames[i], modelDetails[i]);
    	}else 
    		if (inputOption.startsWith("create")) {
    		System.out.println("Creating Business Entity...");
    		juddiManager.createBussinessEntity(businessName, "Description of the business " + businessName);
    	} else if (inputOption.startsWith("publish")) {
    		publishWebService(juddiManager, inputOption);    		    		
    	} else if (inputOption.startsWith("delete")) {
    		System.out.println("Deleting Business Entity...");
    		juddiManager.deleteBusinessEntities(businessName);    		    		
    	}  else if (inputOption.startsWith("print")) {
    		System.out.println("Finding Business Entity...");
    		juddiManager.printBusinessEntities(businessName);
    	} else if (inputOption.startsWith("q")) {
    		isRunning = false;
    	}  
	}
    
    private static void publishWebService(JuddiManager juddiManager, String inputOption) throws DispositionReportFaultMessage, RemoteException {
		String key = "";
		String serviceName = "";
		int indexUrl = 0;
		String [] inputOptionSplitted = null;
		try {
			inputOptionSplitted = inputOption.split(" ");
			key = inputOptionSplitted[1];
			serviceName = inputOptionSplitted[2];
			indexUrl = Integer.parseInt(inputOptionSplitted[3]);
		} catch(Exception ex){
			ex.printStackTrace();
			return;
		}
				
		System.out.println("Publishing Service...");
		juddiManager.publishWebService(key, serviceName, urls[indexUrl], "Description for web service", uri, "ModelName" + serviceName, "Model details for web service");
    }
    
}
